<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/11/25 0025
 * Time: 9:19
 */
namespace app\api\controller;

use app\api\model\Api;
use app\api\model\JWT;
use think\App;
use think\Controller;
use think\Db;
use app\api\model\Role as RoleModel;

/**
 * @title 订单管理
 * @description 接口说明
 * @header name:flag require:1 default: desc:手机设备号
 */
class Order extends Api
{
    /**
     * @title 全部订单
     * @description 接口说明
     * @author 开发者
     * @url /api/order/index
     * @method POST
     * @param name:day type:int require:1 default: other: desc:1-汇总2-30天3-90天4-本年
     * @param name:data type:int require:0 default: other: desc:1预定日期2宴会日期
     * @param name:tid type:int require:0 default: other: desc:类型
     * @param name:bid type:int require:0 default: other: desc:宴会厅
     * @param name:search type:string require:0 default: other: desc:搜索
     * @param name:begin type:string require:0 default: other: desc:开始时间（2021-01-22）
     * @param name:end type:string require:0 default: other: desc:结束时间（2021-01-22）
     * @param name:director_id type:int require:0 default: other: desc:客户经理id
     * 
     * 
     * @return list:列表@
     * @list id:id b_name:宴会厅名称 book:预订人 t_name:宴会类型 mobile:手机号 begin:宴会日期 is_del:1已取消2未取消 addtime:预定时间
     * @return count:场数
     * @return desk:桌数
     * @return deposit:押金
     * @return check:总金额
     * @return total:总条数
     */
    public function index()
    {
        $user = $this->user;
        $day = input('day')??1;
        $datas = input('data');
        $tid = input('tid');
        $bid = input('bid');
        $search = input('search');
        $page = input('page')??1;
        $director_id = input('director_id');
        if($datas == 2){
            $begin = input('begin');
            $end = input('end');
            if($begin && $end){
                $begin = explode("-",$begin);
                $end = explode("-",$end);
                $where[] = ['begin_year','between',[$begin[0],$end[0]]];
                $where[] = ['begin_month','between',[$begin[1],$end[1]]];
                $where[] = ['begin_day','between',[$begin[2],$end[2]]];
            }else{
               
            }
    
        }elseif($datas == 1){
            $begin = input('begin');
            $end = input('end');
            if(!$begin && !$end){
                //当天开始时间
                $begin=strtotime(date("Y-m-d",time()));
                //当天结束之间
                $end=$begin+60*60*24;
                $where[] = ['addtime','between',[$begin,$end]];
            }else{
                $begin = strtotime($begin);
                $end = strtotime($end);
                $where[] = ['addtime','between',[$begin,$end]];
            }
        }
        if($day == 2){
            $begins = mktime(0,0,0,date('m'),1,date('Y'));
            $ends = mktime(23,59,59,date('m'),date('t'),date('Y'));
            $begins = date('Y-m-d H:i:s',$begins);
            $ends = date('Y-m-d H:i:s',$ends);
            $begins = explode("-",$begins);
            $ends = explode("-",$ends);
            
            $where[] = ['begin_year','between',[$begins[0],$ends[0]]];
            $where[] = ['begin_month','between',[$begins[1],$ends[1]]];
            $begins = explode(" ",$begins[2]);
            $ends = explode(" ",$ends[2]);

            $where[] = ['begin_day','between',[$begins[0],$ends[0]]];
        }elseif($day == 3){
            $date = $this->actionQuarter();
            $begins = explode("-",$date['sD']);
            $ends = explode("-",$date['eD']);
            $where[] = ['begin_year','between',[$begins[0],$ends[0]]];
            $where[] = ['begin_month','between',[$begins[1],$ends[1]]];
            $where[] = ['begin_day','between',[$begins[2],$ends[2]]];
        }elseif($day == 4){
            $date = $this->actionYear();
            $begins = explode("-",$date['sD']);
            $ends = explode("-",$date['eD']);
            $where[] = ['begin_year','between',[$begins[0],$ends[0]]];
            $where[] = ['begin_month','between',[$begins[1],$ends[1]]];
            $where[] = ['begin_day','between',[$begins[2],$ends[2]]];
        }
        
        if(is_numeric($search)){
            $where[] = ['mobile','like',"%".$search."%"];
        }else{
            $where[] = ['book','like',"%".$search."%"];
        }
        if($tid){
            $where[] = ['tid','=',$tid];
        }
        if($bid){
            $where[] = ['bid','=',$bid];
        }
        $where[] = ['hid','=',$user['hid']];
        $where[] = ['type','neq',1];
        $where[] = ['is_del','=',2];
        if($director_id){
            $where[] = ['director_id','=',$director_id];
        }
        // print_r($where);die;
        $scheduled = db::name('scheduled')->where($where)
        ->field('id,begin_year,begin_month,begin_day,addtime,begin_night,is_del,b_name,book,t_name,mobile,end_year,end_month,end_day,end_night,is_meeting')
        ->order('is_del')->page("$page,10")->select();
        foreach ($scheduled as $k => $v) {
            // if($v['begin_night'] == 1){
            //     $begin_night = '中午';
            // }else{
            //     $begin_night = '下午';
            // }
            if($v['is_meeting'] == 1){
                // if($v['end_night'] == 1){
                //     $end_night = '中午';
                // }else{
                //     $end_night = '下午';
                // }
                $scheduled[$k]['end'] = $v['end_year'] . '-' .$v['end_month'] . '-' .$v['end_day'];
            }
            $scheduled[$k]['begin'] = $v['begin_year'] . '-' .$v['begin_month'] . '-' .$v['begin_day'];
            $scheduled[$k]['addtime'] =  date('Y-m-d H:i:s',$v['addtime']);
            unset($scheduled[$k]['begin_year']);
            unset($scheduled[$k]['begin_month']);
            unset($scheduled[$k]['begin_day']);
            // unset($scheduled[$k]['begin_night']);
            unset($scheduled[$k]['end_year']);
            unset($scheduled[$k]['end_month']);
            unset($scheduled[$k]['end_day']);
            // unset($scheduled[$k]['end_night']);
        }
        $count = db::name('scheduled')->where($where)->count();
        $total = db::name('scheduled')->where($where)->count();
        $desk = db::name('scheduled')->where($where)->sum('desk');
        // 押金
        if($day == 2){
            $begins = mktime(0,0,0,date('m'),1,date('Y'));
            $ends = mktime(23,59,59,date('m'),date('t'),date('Y'));
            $wap[] = ['addtime','between',[$begins,$ends]];
            $wheres[] = ['addtime','between',[$begins,$ends]];
        }elseif($day == 3){
            $date = $this->actionQuarter();
            $wap[] =  ['addtime','between',[$date['sT'],$date['eT']]];
            $wheres[] =  ['addtime','between',[$date['sT'],$date['eT']]];
        }elseif($day == 4){
            $date = $this->actionYear();
            $begins = strtotime($date['sD']);
            $ends = strtotime($date['eD']);
            $wap[] = ['addtime','between',[$begins,$ends]];
            $wheres[] = ['addtime','between',[$begins,$ends]];
        }else{
            $wap[] = ['hid','=',$user['hid']];
            $wheres[] = ['hid','=',$user['hid']];
        }
        $wap[] = ['hid','=',$user['hid']];
        $wheres[] = ['hid','=',$user['hid']];

        $wap[] = ['manager_id','=',$user['id']];
        $wheres[] = ['manager_id','=',$user['id']];

        
        if($datas == 1){
            $usera = array_column($scheduled, 'begin');
            array_multisort($usera, SORT_ASC, $scheduled);
        }else{
            $usera = array_column($scheduled, 'addtime');
            array_multisort($usera, SORT_ASC, $scheduled);
        }

        $deposit = db::name('scheduled_deposit')->where($wheres)->sum('money');
        // 总金额

        $check = db::name('check_order')->where($wap)->sum('total_money');
        $data['page'] = ceil($total/10);
        $data['list'] = $scheduled;
        $data['count'] = $total;
        $data['desk'] = $desk;
        $data['deposit'] = $deposit;
        $data['check'] = $check;
        $data['total'] = $total;
        return $this->ajaxReturn(0,'成功',$data);
    }
    /**
     * @title 待处理订单
     * @description 接口说明
     * @author 开发者
     * @url /api/order/pending
     * @method POST
     * @param name:day type:int require:1 default: other: desc:1-7天2-30天3-90天4-本年
     * @param name:tid type:int require:0 default: other: desc:类型
     * @param name:bid type:int require:0 default: other: desc:宴会厅
     * @param name:director_id type:int require:0 default: other: desc:客户经理id
     * @param name:page type:int require:0 default: other: desc:页码
     * 
     * 
     * @return list:列表@
     * @list id:id b_name:宴会厅名称 book:预订人 t_name:宴会类型 mobile:手机号 begin:日期 begin_night:1中午2下午 end_night:会议1中午2下午
     * 
     */
    public function pending()
    {
        $user = $this->user;
        $day = input('day');
        $tid = input('tid');
        $bid = input('bid');
        $page = input('page')??1;
        $director_id = input('director_id');
        if(!$day){
            $day = 1;
        }
        if($day == 2){
            $begins = mktime(0,0,0,date('m'),1,date('Y'));
            $ends = mktime(23,59,59,date('m'),date('t'),date('Y'));
            $begins = date('Y-m-d H:i:s',$begins);
            $ends = date('Y-m-d H:i:s',$ends);
            $begins = explode("-",$begins);
            $ends = explode("-",$ends);
            $where[] = ['begin_year','between',[$begins[0],$ends[0]]];
            $where[] = ['begin_month','between',[$begins[1],$ends[1]]];
            $begins = explode(" ",$begins[2]);
            $ends = explode(" ",$ends[2]);
            $where[] = ['begin_day','between',[$begins[0],$ends[0]]];
        }elseif($day == 3){
            $date = $this->actionQuarter();
            $begins = explode("-",$date['sD']);
            $ends = explode("-",$date['eD']);
            $where[] = ['begin_year','between',[$begins[0],$ends[0]]];
            $where[] = ['begin_month','between',[$begins[1],$ends[1]]];
            // $where[] = ['begin_day','between',[$begins[2],$ends[2]]];
            // print_r($where);die;
        }elseif($day == 1){
            $aa = strtotime('-7 days');
            $start_time = date('Y-m-d',$aa);
            $ends_time = date('Y-m-d',time());
            $begins = explode("-",$start_time);
            $ends = explode("-",$ends_time);
            if($begins[2] > $ends[2]){
                $begins[2] = '01';
            }
            $where[] = ['begin_year','between',[$begins[0],$ends[0]]];
            $where[] = ['begin_month','between',[$begins[1],$ends[1]]];
            $where[] = ['begin_day','between',[$begins[2],$ends[2]]];
        }
        // print_r($where);die;
        if($tid){
            $where[] = ['tid','=',$tid];
        }
        if($bid){
            $where[] = ['bid','=',$bid];
        }
        if($director_id){
            $where[] = ['director_id','=',$director_id];
        }
        $where[] = ['hid','=',$user['hid']];
        $where[] = ['type','=',2];
        $where[] = ['is_del','=',2];

        // print_r($where);die;
        $scheduled = db::name('scheduled')->where($where)
        ->field('id,begin_year,begin_month,begin_day,begin_night,b_name,book,t_name,mobile,end_year,end_month,end_day,end_night,is_meeting')
        ->order('is_del')->page("$page,10")->select();
        // print_r($scheduled);die;
        foreach ($scheduled as $k => $v) {
            // if($v['begin_night'] == 1){
            //     $begin_night = '中午';
            // }else{
            //     $begin_night = '下午';
            // }
            if($v['is_meeting'] == 1){
                // if($v['end_night'] == 1){
                //     $end_night = '中午';
                // }else{
                //     $end_night = '下午';
                // }
                $scheduled[$k]['end'] = $v['end_year'] . '-' .$v['end_month'] . '-' .$v['end_day'];
            }
            $scheduled[$k]['begin'] = $v['begin_year'] . '-' .$v['begin_month'] . '-' .$v['begin_day'];
            unset($scheduled[$k]['begin_year']);
            unset($scheduled[$k]['begin_month']);
            unset($scheduled[$k]['begin_day']);
            // unset($scheduled[$k]['begin_night']);
            unset($scheduled[$k]['end_year']);
            unset($scheduled[$k]['end_month']);
            unset($scheduled[$k]['end_day']);
            // unset($scheduled[$k]['end_night']);
        }
        $total = db::name('scheduled')->where($where)->count(); 
        // print_r($scheduled);die;



        $scheduleds = array_column($scheduled, 'begin');
        array_multisort($scheduleds, SORT_ASC, $scheduled);
        $data['page'] = ceil($total/10);
        $data['total'] = $total;
        $data['list'] = $scheduled;
        return $this->ajaxReturn(0,'成功',$data);

        
    }
    /**
     * @title 查看所有分类
     * @description 接口说明
     * @author 开发者
     * @url /api/order/scheduled_type
     * @method POST
     * 
     * 
     * @return list:列表@
     * @list id:id name:分类 
     */
    public function scheduled_type()
    {
        $user = $this->user;
        $type = db::name('banquet_types')->where('hid',$user['hid'])->where('is_del',1)->field('id,name')->select();
        $data['type'] = $type;
        return $this->ajaxReturn(0,'成功',$data);  
    }

    public function actionQuarter()
    {
        $time = time();
        $season = ceil((date('n', $time))/4);
        $startTime = strtotime(date('Y-m-d H:i:s', mktime(0, 0, 0,$season*4-4+1,1,date('Y'))));
        $endTime = strtotime(date('Y-m-d H:i:s', mktime(23,59,59,$season*4,date('t',mktime(0, 0 , 0,$season*4,1,date("Y"))),date('Y'))));
        $data['sD'] = date('Y-m-d',$startTime);
        $data['eD'] = date('Y-m-d',$endTime);
        $data['sT'] = $startTime;
        $data['eT'] = $endTime;
        return $data;
    }
    // 获取年的开始结束时间
	public function actionYear()
    {
        $date = date('Y-m-d H:i:s');
        $startTime = strtotime(date(date("Y-01-01 00:00:00",strtotime("$date"))));
        $endTime = strtotime(date('Y-12-31 23:59:59', strtotime("$date")));
        $data['sD'] = date('Y-m-d',$startTime);
        $data['eD'] = date('Y-m-d',$endTime);
        return $data;
    }
    // 查看客户经理
    public function manager()
    {
        $user = $this->user;
        $users = db::name('user')->where('hid',$user['hid'])->field('id,username')->select();
        return $users;
    }
    /**
     * @title 结单获取已付押金
     * @description 接口说明
     * @author 开发者
     * @url /api/order/check
     * @method POST
     * @param name:id type:int require:1 default: other: desc:订单id
     * 
     * @return list:列表@
     * @list id:id money:金额 image:图片 name:名称
     * @return d_money:列表
     * @return count:总金额
     * @return id:订单id
     */
    public function check()
    {
        $id = input('id');
        $user = $this->user;
        $where[] = ['hid','=',$user['hid']];
        $where[] = ['status','=',2];
        $where[] = ['sid','=',$id];
        $order = db::name('scheduled')->where('id',$id)->find();
        if($order['director_id'] == $user['id']){

        }else{
            $pid = db::name('admin_role')->where('id','in',$user['role'])->field('pid')->select();
            $pids = 0;
            foreach ($pid as $vs) {
                if($vs['pid'] == 0){
                    $pids = 1;
                    break;
                }else{
                    $pids = 2;
                }
            }
            if($pids == 2){
                $res = RoleModel::roleAuth($user['role']);
                $newArrs = [];
                if($res){
                    foreach ($res as $k => $v) {
                        $str1 =str_replace("[","",$res[$k]);
                        $str1 =str_replace("]","",$str1);
                        $newArr = explode(',',$str1);
                        $newArrs = array_merge($newArr,$newArrs);
                    }
                    foreach ($newArrs as $kn => $vn) {
                        $vn =str_replace('"',"",$vn);
                        $newArrs[$kn] = $vn;
                    }
                    if(!in_array('296',$newArrs)){
                        return $this->ajaxReturn(1,'无权结单');
                    }
                }
            }
        }
        $deposit = db::name('scheduled_deposit')->where($where)->field('id,money,image')->select();
        $count = db::name('scheduled_deposit')->where($where)->sum('money');
        $i = 0;
        foreach ($deposit as $k => $v) {
            $name = ['首次','二次','三次','四次','五次','六次','七次','八次','九次','十次'];
            $deposit[$k]['name'] = $name[$i] . '预付款';
            $i++;
            $deposit[$k]['image'] = get_file_path($v['image']);;

        }
        $data['list'] = $deposit;
        $data['d_money'] = $order['d_money'];
        $data['count'] = $count;
        $data['id'] = $order['id'];
        $data['desk'] = $order['desk'];
        return $this->ajaxReturn(0,'成功',$data);
    }
    /**
     * @title 订单结算（暂时不用，用订单修改获取数据接口）
     * @description 接口说明
     * @author 开发者
     * @url /api/order/settlement
     * @method POST
     * @param name:id type:int require:1 default: other: desc:订单id
     * 
     * @return newArr:预留信息@
     * @newArr title:标题 names:姓名 mobile:标题 mobiles:手机号
     * 
     * @return newArrs:预付款或押金@
     * @newArrs gid:担保人 type:1预收款2担保人 num:订单号 image:收据 money:金额 pay_name:支付名称 type_name:类型名称
     * 
     * @return order:订单@
     * @order id:id is_meeting:1会议2不是会议 begin_time:开始时间 end_time:结束时间 b_name:宴会厅 t_name:宴会厅类型 desk:桌数 prepare_desk:备桌 d_name:餐标名称 book:预订人 mobile:预订人手机号 increase_money:增值服务 director:客户经理 source:客户来源 addtime:申请时间 content:备注 contract:合同 other:其他信息 status:1未审核不可结单2已审核可结单
     * 
     * 
     */
    public function settlement()
    {
        $id = input('id');
        if(!$id){
            return $this->ajaxReturn(1,'缺少参数');
        }
        $user = $this->user;
        $where[] = ['id','=',$id];
        $where[] = ['type','=',2];
        $where[] = ['is_del','=',2];

        $order = db::name('scheduled')->where('id',$id)
        ->field('id,begin_year,begin_month,begin_day,begin_night,end_year,end_month,end_day,
        end_night,is_meeting,b_name,t_name,desk,prepare_desk,d_name,book,mobile,sex,increase_money,
        director,source,addtime,content,contract,other,status,tid')->find();
        if(!$order){
            return $this->ajaxReturn(1,'订单为潜客订单');
        }
        if($order['is_meeting'] == 1){
            if($order['end_night'] == 1){
                $end_night = '中午';
            }else{
                $end_night = '晚上';
            }
            $order['end_time'] = $order['end_year'] . '-' . $order['end_month']. '-' . $order['end_day']. ' ' .$end_night;
        }
        if($order['begin_night'] == 1){
            $begin_night = '中午';
        }else{
            $begin_night = '晚上';
        }
        $order['begin_time'] = $order['begin_year'] . '-' . $order['begin_month']. '-' . $order['begin_day']. ' ' . $begin_night;
        unset($order['begin_day']);
        unset($order['begin_year']);
        unset($order['begin_month']);
        unset($order['end_year']);
        unset($order['end_month']);
        unset($order['end_day']);
        unset($order['end_night']);
        unset($order['begin_night']);
        if($order['sex'] == 1){
            $order['book'] = $order['book'] . ' '. '先生';
        }else{
            $order['book'] = $order['book'] . ' '. '女士';
        }
        unset($order['sex']);
        if($order['other']){
            $image = explode( ",",$order['other']);
            if(count($image) != 1){
                $pic = array();
                foreach($image as $vs){
                    $pic[] = get_file_path($vs);
                }
                $order['other'] = $pic;
            }else{
                $pics = array();
                $pics[] = get_file_path($order['other']);
                $order['other'] = $pics;
            }
        }
        if($order['contract']){
            $image = explode( ",",$order['contract']);
            if(count($image) != 1){
                $pic = array();
                foreach($image as $vs){
                    $pic[] = get_file_path($vs);
                }
                $order['contract'] = $pic;
            }else{
                $pics = array();
                $pics[] = get_file_path($order['contract']);
                $order['contract'] = $pics;
            }
        }

        $order['source'] = db::name('source')->where('id',$order['source'])->value('name');
        $order['addtime'] =  date('Y-m-d H:i:s',$order['addtime']);

        $wheres[] = ['tid','=',$order['tid']];
        $wheres[] = ['hid','=',$user['hid']];
        $banquet_name = db::name('banquet_name')->where($wheres)
        ->field('id,name,names')->select();
        unset($order['tid']);
        $newArr = [];
        foreach ($banquet_name as $k => $v) {
            $newArr[$k]['title'] = $v['name'];
            $newArr[$k]['names'] = db::name('banquet_names')->where('bid',$v['id'])->where('did',$id)->value('name');
            $newArr[$k]['mobile'] = $v['names'];
            $newArr[$k]['mobiles'] = db::name('banquet_names')->where('bid',$v['id'])->where('did',$id)->value('value');
        }
        $list = db::name('scheduled_deposit')->where('sid',$id)->where('hid',$user['hid'])->
        field('num,image,money,addtime,pay_name,type_name')->select();
        $guarantee = db::name('guarantee_order')->where('did',$id)->
        field('gid,addtime')->select();
        if($list){
            foreach ($list as $k => $v) {
                $list[$k]['type'] = 1;
                if($v['image']){
                    $list[$k]['image'] = get_file_path($v['image']);
                }
            }
        }
        if($guarantee){
            foreach ($guarantee as $ks => $vs) {
                $guarantee[$ks]['type'] = 2;
                if($vs['gid']){
                    $guarantee[$ks]['gid'] = db::name('guarantee')->where('id',$vs['gid'])->value('name');
                }
            }
        }
        $newArrs = array_merge($list,$guarantee);

        $cmf_arr = array_column($newArrs, 'addtime');
        array_multisort($cmf_arr, SORT_ASC, $newArrs);

        $data['newArr'] = $newArr;
        $data['newArrs'] = $newArrs;
        $data['order'] = $order;
        return $this->ajaxReturn(0,'成功',$data);
    }
     /**
     * @title 确认结单
     * @description 接口说明
     * @author 开发者
     * @url /api/order/check_order
     * @method POST
     * @param name:id type:int require:1 default: other: desc:订单id
     * @param name:desk type:int require:1 default: other: desc:桌数
     * @param name:cou_money type:int require:1 default: other: desc:总额
     * @param name:discount_money type:int require:1 default: other: desc:优惠金额
     * @param name:final_money type:int require:1 default: other: desc:尾款
     *  @param name:content type:string require:1 default: other: desc:备注
     *  @param name:other type:array require:1 default: other: desc:其他信息（name：其他名称，money：其他金额）
     * 
     * 
     */
    public function check_order()
    {
        $id = input('id');
        $user = $this->user;
        $desk = input('desk');
        // 总额
        $cou_money = input('cou_money');
        // 优惠金额
        $discount_money = input('discount_money');
        // 尾款
        $final_money = input('final_money');
        if(!$id || !$desk || !$cou_money ||  !$final_money){
            return $this->ajaxReturn(1,'缺少参数');
        }
        $scheduled = db::name('scheduled')->where('id',$id)->find();
        if($scheduled['director_id'] == $user['id']){

        }else{
            $pid = db::name('admin_role')->where('id','in',$user['role'])->field('pid')->select();
            $pids = 0;
            foreach ($pid as $vs) {
                if($vs['pid'] == 0){
                    $pids = 1;
                    break;
                }else{
                    $pids = 2;
                }
            }
            if($pids == 2){
                $res = RoleModel::roleAuth($user['role']);
                $newArrs = [];
                if($res){
                    foreach ($res as $k => $v) {
                        $str1 =str_replace("[","",$res[$k]);
                        $str1 =str_replace("]","",$str1);
                        $newArr = explode(',',$str1);
                        $newArrs = array_merge($newArr,$newArrs);
                    }
                    foreach ($newArrs as $kn => $vn) {
                        $vn =str_replace('"',"",$vn);
                        $newArrs[$kn] = $vn;
                    }
                    if(!in_array('296',$newArrs)){
                        return $this->ajaxReturn(1,'无权结单');
                    }
                }
            }
        }

        $scheduled_deposit = db::name('scheduled_deposit')->where('sid',$id)->where('status',1)->select();
        $order = db::name('scheduled')->where('id',$id)->find();
        if($scheduled_deposit){
            if($order['begin_night'] == 1){
                $begin_night = '中午';
            }else{
                $begin_night = '晚上';
            }
            $msg = $order['begin_year'] . '-' . $order['begin_month'] . '-' . $order['begin_day']. ' '.  $begin_night . ' ' . $order['b_name'];
            return $this->ajaxReturn(1,"$msg'的订单预付款还没审核通过'");
        }
        $guarantee_order = db::name('guarantee_order')->where('did',$id)->where('status',1)->select();
        if($guarantee_order){
            if($order['begin_night'] == 1){
                $begin_night = '中午';
            }else{
                $begin_night = '晚上';
            }
            $msg = $order['begin_year'] . '-' . $order['begin_month'] . '-' . $order['begin_day']. ' '.  $begin_night . ' ' . $order['b_name'];
            return $this->ajaxReturn(1,"$msg'的订单担保人还没审核通过'");
        }
        
        $other = input('other');

        $where[] = ['hid','=',$user['hid']];
        $where[] = ['status','=',2];
        $where[] = ['sid','=',$id];

        $list['sid'] = $id;
        $list['total_money'] = $cou_money;
        $list['paid'] = db::name('scheduled_deposit')->where($where)->sum('money');
        $list['discount'] = $discount_money;
        $list['tail'] = $final_money;
        $list['content '] = input('content');
        $list['addtime'] = time();
        $list['uid'] = db::name('scheduled')->where('id',$id)->value('uid');
        $list['desk'] = $desk;
        $list['hid'] = $user['hid'];
        $list['uid'] = $scheduled['hid'];

        $list['manager_id'] = $user['id'];
        $cid = db::name('check_order')->insertGetId($list);
        if($other){
            $other = json_decode($other);
            foreach ($other as $k => $v) {
                $res['cid'] = $cid;
                $res['hid'] = $user['hid'];
                $res['name'] = $v->name;
                $res['money'] = $v->money;
                $res['addtime'] = time();
                $res['uid'] = $list['uid'];
                $res['manager_id'] = $user['id'];
                db::name('other_order')->insert($res);
            }
        }
        db::name('client')->where('id',$list['uid'])->setInc('num',1);
        db::name('client')->where('id',$list['uid'])->setInc('money',$cou_money);
        db::name('scheduled')->where('id',$id)->update(['status'=>3,'knot_name'=>time(),'type'=>3]);
        return $this->ajaxReturn(0,'成功');
    }
}